package org.soulraven.games.memory.engine;

import org.apache.log4j.Logger;
import org.soulraven.games.memory.model.Player;

public class TwoPlayersEngine extends BaseMemoryEngine {

	private static final Logger LOGGER = Logger.getLogger(TwoPlayersEngine.class);

	@Override
	public void processEndGame() {
		Player winner = getWinner();
		if (winner != null) {
			gameFrame.setWinner(winner);
		}
		else {
			gameFrame.setTie();
		}
	}

	private Player getWinner() {
		final Player playerOne = players.get(0);
		final Player playerTwo = players.get(1);
		if (playerOne.getScore() == playerTwo.getScore()) {
			return null;
		}
		int winnerIndex = playerOne.getScore() > playerTwo.getScore() ? 0 : 1;
		return players.get(winnerIndex);
	}

	@Override
	public void matchesNotFound() {
		LOGGER.debug("Player changed");
		currentPlayerIndex = 1 - currentPlayerIndex;
		gameFrame.updateStatusBar(getCurrentPlayer());
		super.matchesNotFound();
	}
}
